package com.songcha.library_common.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.widget.EditText

@SuppressLint("AppCompatCustomView")
class CustomEditText : EditText {
    companion object{
        private val DEFAULT_TEXT_SIZE=16f
    }

    //cannot invoke super(context, attrs, defStyleAttr, defStyleRes) in here,otherwise cannot input
    constructor(context: Context?) : super(context,null){
        initView(context,null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
        initView(context,attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initView(context,attrs)
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes){
        initView(context,attrs)
    }

    @SuppressLint("ResourceType")
    fun initView(context: Context?, attrs: AttributeSet?){
        background=null

        if(context!=null){
            val paddingArray=intArrayOf(android.R.attr.padding,android.R.attr.paddingLeft, android.R.attr.paddingTop, android.R.attr.paddingRight, android.R.attr.paddingBottom)
            val taPadding = context.obtainStyledAttributes(attrs, paddingArray)
            val padding=taPadding.getDimensionPixelOffset(0,-1)
            var paddingLeft=taPadding.getDimensionPixelOffset(1,-1)
            var paddingTop=taPadding.getDimensionPixelOffset(2,-1)
            var paddingRight=taPadding.getDimensionPixelOffset(3,-1)
            var paddingBottom=taPadding.getDimensionPixelOffset(4,-1)
            taPadding.recycle()

            if(padding>-1){
                setPadding(padding,padding,padding,padding)
            }else{
                if(paddingLeft==-1) paddingLeft = 0
                if(paddingTop==-1) paddingTop = 0
                if(paddingRight==-1) paddingRight = 0
                if(paddingBottom==-1) paddingBottom = 0
                setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom)
            }

            /*if(padding==-1 && paddingLeft==-1 && paddingTop==-1 && paddingRight==-1 && paddingBottom==-1){
                setPadding(0,0,0,0)
            }*/

            var hasTextSize=false
            if(attrs!=null){
                hasTextSize=attrs.getAttributeValue("http://schemas.android.com/apk/res/android","textSize")!=null
            }

            if(!hasTextSize)
                textSize=(DEFAULT_TEXT_SIZE)
        }

    }

}
